package com.gongpingjialoan.adapter.car;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.RecordBean;

import java.util.List;

/**
 * Created by Administrator on 2016/6/22.
 */
public class FindCarAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private Context context;

    public List<RecordBean.DataEntity.HelpFindCarEntity> mCarSourceList;


    public FindCarAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<RecordBean.DataEntity.HelpFindCarEntity> CarSourceList) {
        this.mCarSourceList = CarSourceList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mCarSourceList == null) {
            return 0;
        }
        return mCarSourceList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCarSourceList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final CarSourceViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.find_car_layout, null);
            holder = new CarSourceViewHolder();
            holder.brandname = (TextView) convertView.findViewById(R.id.brand);
            holder.controlTextView = (TextView) convertView.findViewById(R.id.control);
            holder.tvCarPrice = (TextView) convertView.findViewById(R.id.price);
            holder.volumeTextView = (TextView) convertView.findViewById(R.id.volume);
            holder.pushTypeTextView = (TextView) convertView.findViewById(R.id.push);
            convertView.setTag(holder);
        } else {
            holder = (CarSourceViewHolder) convertView.getTag();
        }

        RecordBean.DataEntity.HelpFindCarEntity helpFindCarEntity = mCarSourceList.get(position);
        if ("".equals(helpFindCarEntity.getBrand_model_zh())) {
            holder.brandname.setText("不限");
        } else {
            holder.brandname.setText(helpFindCarEntity.getBrand_model_zh());
        }

        if (helpFindCarEntity.getControl() == null ) {
            holder.controlTextView.setText("不限");
        }else{
            List<String> controls = helpFindCarEntity.getControl();
            StringBuilder otherStringBuilder = new StringBuilder();
            for (int i = 0; i < controls.size(); i++) {
                if (i == controls.size() - 1) {
                    otherStringBuilder.append(controls.get(i));
                } else {
                    otherStringBuilder.append(controls.get(i) + ",");
                }
            }

            holder.controlTextView.setText(TextUtils.isEmpty(otherStringBuilder.toString()) ? "不限" : otherStringBuilder.toString());
        }

        List<List<String>> prices = helpFindCarEntity.getPrice_ranges();
        if (prices.size() == 1) {
            if (prices.get(0).size() >= 2) {
                if (!TextUtils.isEmpty(prices.get(0).get(0)) && !TextUtils.isEmpty(prices.get(0).get(1))) {
                    holder.tvCarPrice.setText(prices.get(0).get(0) + "-" + prices.get(0).get(1) + "万");
                } else if (!TextUtils.isEmpty(prices.get(0).get(0)) && TextUtils.isEmpty(prices.get(0).get(1))) {
                    holder.tvCarPrice.setText(prices.get(0).get(0) + "万以上");
                } else if(TextUtils.isEmpty(prices.get(0).get(0)) && !TextUtils.isEmpty(prices.get(0).get(1))){
                    holder.tvCarPrice.setText(prices.get(0).get(1) + "万以内");
                }else{
                    holder.tvCarPrice.setText("不限");
                }
            } else {
                holder.tvCarPrice.setText("不限");
            }
        } else {
            holder.tvCarPrice.setText("不限");
        }

        StringBuilder sb = new StringBuilder();
        List<List<String>> volumes = helpFindCarEntity.getVolume_ranges();
        int size = volumes.size();
        for (int i = 0; i < size; i++) {
            List<String> item = volumes.get(i);
            String low = item.get(0);
            String high = item.get(1);
            if(!TextUtils.isEmpty(low) && !TextUtils.isEmpty(high)){
                if(i == size - 1){
                    sb.append(low).append("-").append(high).append("升");
                }else{
                    sb.append(low).append("-").append(high).append("升,");
                }
            }else if(TextUtils.isEmpty(low) && !TextUtils.isEmpty(high)){
                if(i == size - 1){
                    sb.append(high).append("升以内");
                }else{
                    sb.append(high).append("升以内,");
                }
            }else if(!TextUtils.isEmpty(low) && TextUtils.isEmpty(high)){
                if(i == size - 1){
                    sb.append(low).append("升以上");
                }else{
                    sb.append(low).append("升以上,");
                }
            }
        }

        if(TextUtils.isEmpty(sb.toString())){
            holder.volumeTextView.setText("不限");
        }else{
            holder.volumeTextView.setText(sb.toString());
        }

        holder.pushTypeTextView.setText("找到立即推送");

        return convertView;

    }

    public class CarSourceViewHolder {
        public TextView tvCarPrice;
        public TextView brandname;
        public TextView controlTextView;
        public TextView volumeTextView;
        public TextView pushTypeTextView;
    }
}
